package com.cryptoneedle.center.source.entity.metadata;


import cn.hutool.v7.core.bean.BeanUtil;
import com.bubbles.engine.data.core.entity.BaseEntity;
import com.cryptoneedle.center.source.enums.DorisDataType;
import com.cryptoneedle.center.source.key.metadata.ColumnKey;
import com.cryptoneedle.center.source.key.metadata.DatabaseKey;
import com.cryptoneedle.center.source.key.metadata.TableKey;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Comment;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>description: 源数据源-列 </p>
 *
 * @author CryptoNeedle
 * @date 2025-06-19
 */
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@jakarta.persistence.Table(schema = "public", name = "source_column")
@IdClass(ColumnKey.class)
@Comment("源数据源-列")
public class Column extends BaseEntity {

    @Id
    @Comment("源数据源-目录名称")
    private String catalogName;
    @Id
    @Comment("源数据源-数据库名称")
    private String databaseName;
    @Id
    @Comment("源数据源-表名称")
    private String tableName;
    @Id
    @Comment("源数据源-列名称")
    private String columnName;

    @jakarta.persistence.Column(columnDefinition = "TEXT")
    @Comment("源数据源-列评论")
    private String comment;
    @Comment("源数据源-排序")
    private Long position;

    @Comment("源数据源-数据类型")
    private String dataType;
    @Comment("源数据源-数据长度")
    private Long length;
    @Comment("源数据源-数据精度")
    private Long precision;
    @Comment("源数据源-数据标度")
    private Long scale;
    @Comment("源数据源统计信息-表数据量")
    private Long rowCount;
    @Comment("源数据源统计信息-采样数量")
    private Long sampleCount;
    @Comment("源数据源统计信息-采样率")
    private BigDecimal sampleRate;
    @Comment("源数据源统计信息-采样空值数量")
    private Long nullCount;
    @Comment("源数据源统计信息-采样不同值数量")
    private Long distinctCount;
    @jakarta.persistence.Column(precision = 21, scale = 20)
    @Comment("源数据源统计信息-采样数据密度")
    private BigDecimal density;
    @jakarta.persistence.Column(columnDefinition = "TEXT")
    @Comment("源数据源统计信息-采样最小值")
    private String minValue;
    @jakarta.persistence.Column(columnDefinition = "TEXT")
    @Comment("源数据源统计信息-采样最大值")
    private String maxValue;
    @jakarta.persistence.Column(precision = 30, scale = 20)
    @Comment("源数据源统计信息-列平均字节数")
    private BigDecimal avgDataSizeByte;
    @Comment("源数据源统计信息-预估占用空间(单位：MB)")
    private BigDecimal estimateMegaBytes;
    @Comment("源数据源统计信息-统计时间")
    private Date statisticDt;

    @Comment("源数据源-Doris目录编码")
    private String dorisCatalogName;
    @Comment("源数据源-系统编码")
    private String systemCode;
    @Comment("源数据源-启用列")
    private Boolean enabled;

    @Comment("转换-表名称")
    private String transTableName;
    @Comment("转换-列名称")
    private String transColumnName;
    @Comment("源数据源-人工指定列评论")
    private Boolean transColumnNameManual;
    @jakarta.persistence.Column(columnDefinition = "TEXT")
    @Comment("转换-列评论")
    private String transComment;
    @Comment("源数据源-人工指定列名称")
    private Boolean transCommentManual;


    @Enumerated(EnumType.STRING)
    @Comment("转换-数据类型")
    private DorisDataType transDataType;
    @Comment("转换-数据长度")
    private Long transLength;
    @Comment("转换-数据精度")
    private Long transPrecision;
    @Comment("转换-数据标度")
    private Long transScale;
    @Comment("转换-排序")
    private Long transPosition;
    @Comment("转换-提取需通过to_char()转换")
    private Boolean transToChar;
    @jakarta.persistence.Column(length = 4000)
    @Comment("转换-警告")
    private String transWarn;
    @jakarta.persistence.Column(length = 4000)
    @Comment("转换-异常")
    private String transError;
    @Comment("转换-预估占用字节")
    private Double transEstimateBytes;

    /**
     * Key
     */

    public DatabaseKey databaseKey() {
        return BeanUtil.copyProperties(this, DatabaseKey.class);
    }

    public TableKey tableKey() {
        return BeanUtil.copyProperties(this, TableKey.class);
    }

    public ColumnKey columnKey() {
        return BeanUtil.copyProperties(this, ColumnKey.class);
    }

    /**
     * 其他
     */
    public String transFullDataType() {
        String fullDataType = this.transDataType.toString();
        if (DorisDataType.DECIMAL.equals(this.transDataType)) {
            fullDataType = fullDataType + "(" + this.precision + ", " + this.scale + ")";
        } else if (DorisDataType.DATETIME.equals(this.transDataType)) {
            if (this.scale != null) {
                fullDataType = fullDataType + "(" + this.scale + ")";
            }
        } else if (DorisDataType.CHAR.equals(this.transDataType)) {
            fullDataType = fullDataType + "(" + this.length + ")";
        } else if (DorisDataType.VARCHAR.equals(this.transDataType)) {
            fullDataType = fullDataType + "(" + this.length + ")";
        }
        return fullDataType;
    }
} 